package base.binarysearch;

class Solution {

    // 二分查找, 有序的数组
    public int binarySearch(int[] arr, int obj) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == obj) {
                return mid;
            } else if (arr[mid] < obj) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
}
